package jdbcmanager.print.preview;

import java.awt.*;
import javax.swing.border.*;
import javax.swing.*;

/**
 * Renderizes a single page.
 */

final class PagePreview extends JPanel
{
	protected int   nWidth;
	protected int   nHeight;
	protected Image imSource;
	protected Image img;

   PagePreview( int w, int h, Image imSourc )
	{
		this.imSource = imSourc;
		this.nWidth   = w;
		this.nHeight  = h;
		this.img      = imSource.getScaledInstance( nWidth, nHeight, Image.SCALE_SMOOTH );
		this.img.flush();
		setBackground( Color.white );
		setBorder( new MatteBorder( 1, 1, 2, 2, Color.black ) );
	}

	public void setScaledSize( int nWidth, int nHeight )
	{
		this.nWidth  = nWidth;
		this.nHeight = nHeight;
		this.img = imSource.getScaledInstance( nWidth, nHeight, Image.SCALE_SMOOTH );
		repaint();
	}

	public Dimension getPreferredSize()
	{
		Insets ins = getInsets();
		return new Dimension( this.nWidth + ins.left + ins.right, this.nHeight + ins.top + ins.bottom );
	}

	public Dimension getMaximumSize()
	{
		return getPreferredSize();
	}

	public Dimension getMinimumSize()
	{
		return getPreferredSize();
	}

	public void paint( Graphics gr )
	{
		gr.setColor( getBackground() );
		gr.fillRect( 0, 0, getWidth(), getHeight() );
		gr.drawImage( this.img, 0, 0, this );
		paintBorder( gr );
	}
}
